#include <typeConvert.h>

static int isBigSystem()
{
    static DEV_C51_CODE_FLAG const union
    {
        uint16_t b;
        uint8_t a;
    } checkval = {0x0102};
    return (checkval.a == 0x01);
}
static void ovtTypeCom(void *valuepointer, int valuesize)
{
    char *tmppointer = (char *)valuepointer;
    int i;
    if (!valuepointer)
    {
        logerror("");
        return;
    }
    if (valuesize % 2)
    {
        logerror("%d", valuesize);
        return;
    }
    for (i = 0; i < valuesize / 2; i++)
    {
        SWAP_BASE_VAL(tmppointer[i], tmppointer[valuesize - 1 - i]);
    }
}

uint16_t ovtBigSys16(int16_t value)
{
    if (isBigSystem())
    {
        ovtTypeCom(&value, sizeof(int16_t));
    }
    return value;
}
uint16_t ovtLitSysI16(int16_t value)
{
    if (!isBigSystem())
    {
        ovtTypeCom(&value, sizeof(int16_t));
    }
    return value;
}

uint32_t ovtBigSys32(uint32_t value)
{
    if (isBigSystem())
    {
        ovtTypeCom(&value, sizeof(uint32_t));
    }
    return value;
}
uint32_t ovtLitSys32(uint32_t value)
{
    if (!isBigSystem())
    {
        ovtTypeCom(&value, sizeof(uint32_t));
    }
    return value;
}

float ovtBigSysFloat(float value)
{
    if (isBigSystem())
    {
        ovtTypeCom(&value, sizeof(float));
    }
    return value;
}
float ovtLitSysFloat(float value)
{
    if (!isBigSystem())
    {
        ovtTypeCom(&value, sizeof(float));
    }
    return value;
}

#if DEVICE_PLATFORM_CONFIGURE != DEVICE_PLATFORM_C51_MCU
uint64_t ovtBigSys64(uint64_t value)
{
    if (isBigSystem())
    {
        ovtTypeCom(&value, sizeof(uint64_t));
    }
    return value;
}
uint64_t ovtLitSys64(uint64_t value)
{
    if (!isBigSystem())
    {
        ovtTypeCom(&value, sizeof(uint64_t));
    }
    return value;
}

double ovtBigSysDble(double value)
{
    if (isBigSystem())
    {
        ovtTypeCom(&value, sizeof(double));
    }
    return value;
}
double ovtLitSysDble(double value)
{
    if (!isBigSystem())
    {
        ovtTypeCom(&value, sizeof(double));
    }
    return value;
}
#endif
